package com.service.impl;

import com.entity.Book;
import com.github.pagehelper.PageInfo;
import com.mapper.BookMapper;
import com.mapper.ItemsMapper;
import com.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("bookService")
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private ItemsMapper itemsMapper;
    @Override
    public void insertBook(Book book) {
        bookMapper.insertBook(book);
    }

    @Override
    public void deleteBookById(Integer book_id) {
        if(itemsMapper.countRowsByBookId(book_id) > 0){
            throw new RuntimeException("该图书已有销量，不能删除");
        }
        bookMapper.deleteBookById(book_id);
    }

    @Override
    public void updateBook(Book book) {
        bookMapper.updateBook(book);
    }

    @Override
    public Book selectBookById(Integer id) {
        return bookMapper.selectBookById(id);
    }

    @Override
    public Book selectBookByName(String name) {
        return bookMapper.selectBookByName(name);
    }

    @Override
    public PageInfo<Book> searchBooks(@Param("pageNum")Integer pageNum,
                                     @Param("pageSize")Integer pageSize,
                                     @Param("value") String value) {
        return new PageInfo<>(bookMapper.selectPageBook(pageNum,pageSize,null,value),10);
    }

    @Override
    public PageInfo<Book> selectPageBook(Integer pageNum, Integer pageSize, String query, String value) {
        return new PageInfo<>(bookMapper.selectPageBook(pageNum,pageSize,query,value),10);
    }

    @Override
    public List<Book> showRecommendBooks() {
        return bookMapper.recommendBooks();
    }

    @Override
    public List<Book> showHotSellBooks() {
        return bookMapper.hotSellBooks();
    }

    @Override
    public List<Book> showNewBooks() {
        return bookMapper.newBooks();
    }

    @Override
    public List<Book> showNewHot() {
        return bookMapper.newHotSellBooks();
    }

    @Override
    public PageInfo<Book> showBooksByCategory( Integer pageNum, Integer pageSize,Integer fid, Integer sid) {
        return new PageInfo<>(bookMapper.selectBookByCategory(pageNum,pageSize,fid,sid));
    }
}
